<template>
    <div>
        <!--model:表单数据-->
        <el-form label-width="80px" :model="menuForm" :rules="rules" ref="formref">

            <el-form-item label="菜单名称" prop="menuName">
                <el-input v-model="menuForm.menuName"></el-input>
            </el-form-item>
            <el-form-item label="url" prop="url">
                <el-input v-model="menuForm.url"></el-input>
            </el-form-item>
            <el-form-item label="说明">
                <el-input v-model="menuForm.remark"></el-input>
            </el-form-item>
            <el-form-item label="父菜单"  v-show="menuForm.parentId!=0">
                <el-select v-model="menuForm.parentId"   placeholder="请选择">
                    <el-option
                            v-for="item in menulist"
                            :key="item.menuId"
                            :label="item.menuName"
                            :value="item.menuId"
                    >
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="图标">
                <div style="margin-top: 15px;">
                    <el-input placeholder="请输入内容" v-model="menuForm.icon"  class="input-with-select" readonly>
                        <el-select v-model="menuForm.icon"   slot="prepend" placeholder="请选择">
                            <el-option v-for="item in  iconlist" :key="item.unicode" :label="item.unicode" :value="item.name">
                                <span :class="'iconFont'+' '+'clms'+item.font_class">{{item.name}}</span>
                            </el-option>
                        </el-select>
                    </el-input>
                </div>

            </el-form-item>

        </el-form>
        <span class="iconFont clmsauto"></span>
        <span slot="footer" class="dialog-footer">
                <el-button @click="changestatus()">取 消</el-button>
                <el-button type="primary" @click=" confirmEditmenu()">确 定</el-button>
        </span>
        <el-dialog
                title="提示"
                :visible.sync="dialogVisible"
                width="30%"
                :before-close="handleClose">
            <span>这是一段信息</span>
            <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
  </span>
        </el-dialog>
    </div>
</template>
<script>
    import Icon from '../../assets/icon/iconfont.json'
    let datajson = Icon;
    export default {
        name: "MenuAddOrupdata",
        props: ["menuId", "status"],
        model: {//绑定到父组件中
            prop: 'status',
            event: 'changestust'
        },
        data() {
            var Id = this.menuId
            var same = {id: '', same: ''}
            var that = this
            //唯一性校验
            var validatorurl = function (rule, value, callback) {
                same.id = Id;
                same.same = value;
                that.$http.post(`/system/menu/onlyonecheck/byurl`, same).then(function (response) {
                    console.log(response.data)
                    if (response.data.result.type) {
                        callback();
                    } else {
                        callback("url重复");
                    }
                });
            }
            var validatormenuname = function (rule, value, callback) {
                same.id = Id;
                same.same = value;
                that.$http.post(`/system/menu/onlyonecheck/bymenuname`, same).then(function (response) {
                    if (response.data.result.type) {
                        callback();
                    } else {
                        callback("菜单名重复");
                    }
                });
            }
            return {
                menuForm: {menuId: 0},
                iconlist: [],
                menulist: [],
                dialogVisible: false,
                same: {id: '', name: ''},
                rules: {
                    menuName: [
                        {required: true, message: '菜单不为空', trigger: 'blur'},
                        {min: 2, message: '最少两个字符', trigger: 'blur'},
                        {validator: validatormenuname, trigger: 'blur'},
                    ],
                    url: [
                        { message: '菜单不为空', trigger: 'blur'},
                        {min: 3,  message: '长度最少3 ', trigger: 'blur'},
                        {validator: validatorurl, trigger: 'blur'},
                    ]
                }
            }

        },
        created() {
            this.getmenuinfo();
            this.getmenulist();
            this.Iconget()
        },
        methods: {
            handleClose() {
            },

            //选择图标
            selecticon() {
                this.dialogVisible = true
            },

            Iconget() {
                this.iconlist = datajson.glyphs
            },
            //关闭窗口
            closewindow() {
                this.$emit("changestust", !this.status)
            },
            changestatus() {
                this.closewindow()
            },
            confirmEditmenu() {
                this.$refs.formref.validate((valid) => {
                    if (valid) {
                        var that = this
                        this.$http.post('system/menu/addorupdate', this.menuForm).then(function (response) {
                            that.$message.success(response.data.message);
                            that.closewindow()
                        });
                    }
                location.reload()
                })
            },
            getmenulist() {
                var that = this;
                this.$http.get(`/system/menu/menulist`).then(function (response) {
                    that.menulist = response.data.result.menulist;
                })
            },
            getmenuinfo() {
                if (this.menuId == '' || this.menuId == undefined) {
                } else {
                    var that = this;
                    var menuId = this.menuId;
                    this.$http.get(`system/menu/getmenubyid/${this.menuId}`).then(function (response) {
                        that.menuForm = response.data.result.record;
                    });
                }
            }
        }
    }
</script>

<style scoped>

</style>